<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Course;
use App\Models\CourseClass;
use App\User;
use App\Models\Article;
use App\Models\Studying;
use App\Models\UserGrowing;
use App\Models\FeedBack;
use App\Models\Consultation;
class HomeController extends Controller
{
    public function index(Request $request){

        $data['totalUser'] = User::count();
        $data['registerUser'] = User::where('mobile','!=','')->count();
        $data['authorUser'] = User::where('openid','!=','')->count();

        $data['totalCourse'] = CourseClass::count();
        $data['onlineCourse'] = CourseClass::where('state',1)->count();
        $data['outlineCourse'] = $data['totalCourse'] - $data['onlineCourse'];
        $data['totalArticle'] = Article::count();
        $data['onlineArticle'] = Article::where('state',1)->count();
        $data['outlineArticle'] = $data['totalArticle'] - $data['onlineArticle'];
        $data['studyingUser'] = Studying::count();
        $data['feedBackUser'] = FeedBack::where('status',0)->count();
        $data['consultUser'] = Consultation::where('state',0)->count();
        $result = $this->getUserGrowInfo();
        $data['dateArr'] = $result[0];
        $data['oneApi'] = $result[1];
        $data['twoApi'] = $result[2];
        $data['userApi'] = $result[3];
        $data['userRegApi'] = $result[4];
//        dd($data);
        return view('home.index',$data);
    }

    public function getUserGrowInfo(){
        $j = date("t"); //获取当前月份天数
        $start_time = strtotime(date('Y-m-01'));  //获取本月第一天时间戳
        $array = [];
        $currentTime = strtotime(date('Y-m-d'));
        for($i=0;$i<$j;$i++){
            $nextTime = $start_time+$i*86400;
            if($nextTime <= $currentTime){
                $dateArr[] = date('m-d',$start_time+$i*86400); //每隔一天赋值给数组
                $apiOne = UserGrowing::where('api_number','like','api1%')->where('updated_at','>=',date('Y-m-d',$start_time+$i*86400))->where('updated_at','<',date('Y-m-d',$nextTime+86400))->where('code','00')->distinct('user_id')->count();
                $apiTwo = UserGrowing::where('api_number','like','api2%')->where('updated_at','>=',date('Y-m-d',$nextTime = $start_time+$i*86400))->where('updated_at','<',date('Y-m-d',$nextTime+86400))->where('code','00')->distinct('user_id')->count();
                $apiUser = UserGrowing::where('updated_at','>=',date('Y-m-d',$nextTime = $start_time+$i*86400))->where('updated_at','<',date('Y-m-d',$nextTime+86400))->distinct('user_id')->count();
                $apiRegisterUser = UserGrowing::where('updated_at','>=',date('Y-m-d',$start_time+$i*86400))->where('updated_at','<',date('Y-m-d',$nextTime+86400))->where('mobile','!=','')->distinct('user_id')->count();
                $oneApi[] = $apiOne;
                $twoApi[] = $apiTwo;
                $userApi[] = $apiUser;
                $userRegApi[] = $apiRegisterUser;
            }
        }
        return [$dateArr,$oneApi,$twoApi,$userApi,$userRegApi];
    }
}